package com.uuah.server.config.spring.parsers.generic;

import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.uuah.server.config.spring.parsers.assembly.IBeanAssembler;
import com.uuah.utils.StringUtils;

/**
 * Grabs the text from an element and injects it into the parent, for example:
 * 
 * <foo> <bar-text>A bunch of text.</bar-text> </foo>
 * 
 * registerBeanDefinitionParser("foo", new OrphanDefinitionParser(Foo.class));
 * registerBeanDefinitionParser("bar-text", new
 * TextDefinitionParser("barText"));
 * 
 * will result in a call to Foo.setBarText("A bunch of text.")
 */
public class TextDefinitionParser extends ChildDefinitionParser {
	public TextDefinitionParser(String setterMethod) {
		super(setterMethod, String.class);
	}

	// @Override
	protected void postProcess(ParserContext context, IBeanAssembler assembler,
			Element element) {
		Node node = element.getFirstChild();
		if (node != null) {
			String value = node.getNodeValue();
			if (!StringUtils.isBlank(value)) {
				assembler.getTarget().getPropertyValues().addPropertyValue(
						setterMethod, value);
			}
		}
	}
}
